package com.hilaver.dzmis.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.order.OrderProduct;
import com.hilaver.dzmis.order.OrderProductItem;
import com.hilaver.dzmis.product.ProductDZColor;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductLaunching;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class OrderProductItemServiceImpl extends AbstractBaseService {
	
	private CtServiceImpl ctService;
	private ProductDZColorServiceImpl productDZColorService;

	public OrderProductItemServiceImpl(){
		this.ctService = new CtServiceImpl();
		this.productDZColorService = new ProductDZColorServiceImpl();
	}
	
	@Override
	public String edit(Object obj) throws Exception {
		this.dao.saveOrUpdate(obj);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		return super.delete(OrderProductItem.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		OrderProductItem opItem = (OrderProductItem)this.dao.get(OrderProductItem.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(opItem));
		
		xmlSB.append(this.toXMLFragment(opItem));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		OrderProduct op = (OrderProduct)this.dao.get(OrderProduct.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append("<ctSeasonNumber>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), op.getCtSeasonNumber(), this.locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSeasonNumber>");
		
		xmlSB.append("<ctStatureCode>");
		xmlSB.append(op.getProductIdentification().getCtStature());
		xmlSB.append("</ctStatureCode>");
		
		//xmlSB.append(this.toXMLFragment(op));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String validate(OrderProductItem obj) throws Exception{
		OrderProductItem opi = (OrderProductItem)this.dao.get(OrderProductItem.class.getName(), obj.getId());
		opi.setScheduleDate(obj.getScheduleDate());
		opi.setStockDate(obj.getStockDate());
		opi.setQuantityConsistent(obj.getQuantityConsistent());
		opi.setQuantityNotConsistent(obj.getQuantityNotConsistent());
		opi.setBiFactory(obj.getBiFactory());
		if(opi.getStockDate() != null){
			opi.setIsStock("01");
		}else{
			opi.setIsStock("02");
		}
		
		
		boolean isInprocess2 = false;
		Set items = opi.getOrderProduct().getOrderProductItems();
		for (Iterator iterator = items.iterator(); iterator.hasNext();) {
			OrderProductItem object = (OrderProductItem) iterator.next();
			if("02".equals(object.getIsStock()) || object.getIsStock() == null){
				isInprocess2 = true;
				break;
			}
		}
		ProductIdentification pi = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), opi.getOrderProduct().getProductIdentification().getId());
		if(isInprocess2){
			pi.setIsInprocess2("01");
		}else{
			pi.setIsInprocess2("02");
		}
		
		this.dao.saveOrUpdate(opi);
		this.dao.saveOrUpdate(pi);
		return "{success: true}";
	}
	
	public String getAll(Integer orderProductId) throws Exception{
		String [] filters = new String[1];
		filters[0] = "orderProduct.id = " + orderProductId;
		List<OrderProductItem> opItemList = this.dao.getAll(OrderProductItem.class.getName(), "id", "ASC", filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (OrderProductItem obj : opItemList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append(this.toXMLFragment((OrderProductItem)obj));
			
			if(obj.getBiFactory() != null){
				xmlSB.append("<biFactoryId>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getId());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</biFactoryId>");
				
				xmlSB.append("<factoryNameCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameCN>");
				
				xmlSB.append("<factoryNameEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameEN>");
			}else{
				xmlSB.append("<biFactoryId>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append("");
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</biFactoryId>");
				
				xmlSB.append("<factoryNameCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append("");
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameCN>");
				
				xmlSB.append("<factoryNameEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append("");
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameEN>");
			}
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(opItemList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllVariant(Integer orderProductItemId) throws Exception{
		OrderProductItem opItem = (OrderProductItem)this.dao.get(OrderProductItem.class.getName(), orderProductItemId);
		Set<ProductDZColor> productDZColors = opItem.getOrderProduct().getProductIdentification().getProductDZColors();
		
		List<ProductDZColor> pDZColorList = new ArrayList<ProductDZColor>();
		
		for (Iterator<ProductDZColor> iterator = productDZColors.iterator(); iterator.hasNext();) {
			ProductDZColor i =  iterator.next();
			if(i.getVariant().equals(opItem.getProductDZColor().getVariant())){
				pDZColorList.add(i);
			}
		}
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		this.productDZColorService.setRealFileDir(realFileDir);
		for(ProductDZColor pDZColor : pDZColorList){
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(pDZColor));
			xmlSB.append(this.productDZColorService.toXMLFragment(pDZColor));
			
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(pDZColorList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String toXMLFragment(OrderProductItem opItem) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
		
		xmlSB.append("<productDZColorId>");
		xmlSB.append(opItem.getProductDZColor().getId());
		xmlSB.append("</productDZColorId>");
		
		xmlSB.append("<variant>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(opItem.getProductDZColor().getVariant());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</variant>");
		
		xmlSB.append("<reference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(opItem.getProductDZColor().getProductIdentification().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</reference>");
		
		Set<ProductDZColor> pDZColors = opItem.getOrderProduct().getProductIdentification().getProductDZColors();
		Double percentage = 0d;
		Double actureWeight = 0d;
		String variant = opItem.getProductDZColor().getVariant();
		for (Iterator<ProductDZColor> iterator = pDZColors.iterator(); iterator.hasNext();) {
			ProductDZColor i = iterator.next();
			if(variant.equals(i.getVariant())){
				percentage += i.getPercentage();
				actureWeight += i.getActualWeight() * opItem.getTotal();
			}
			
		}
		
		xmlSB.append("<percentage>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(percentage);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</percentage>");
		
		xmlSB.append("<actureWeight>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(actureWeight);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</actureWeight>");
		
		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		OrderProductItemServiceImpl os = new OrderProductItemServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}

}
